package com.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import com.android.gui.CanvasDrawingActivity;
import com.android.gui.GuesserSideActivity;
import com.android.networking.connection.Connection;
import com.android.networking.connection.Words;
import com.android.networking.setup.Scan;
import com.android.networking.setup.ServerDetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class GuessASketch extends Activity {
	static Connection conn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //this code is for main server which will open server socket, and keep listening
        // on new client connection and maintain the connection pool. 
        //Log.d("GuetchServer","Server started..");
		//conn = Connection.getInstanceOfConnection();
		//ServerSocket sock = conn.openServerSocket();
		//conn.connectionWait();
        if(Words.isIs_answer_correct()==true)
        {        	
        	CharSequence text = "Player " + Words.getCorrect_client() + " Wins";    
        	Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
        	toast.show();
        	Words.setCorrect_client(null);
        	Words.setIs_answer_correct(false);
        }
        findViewById(R.id.hostGame).setOnClickListener(
                new OnClickListener() {
            public void onClick(View v) {
                hostGame(true);
            }
        });
        findViewById(R.id.joinGame).setOnClickListener(
                new OnClickListener() {
            public void onClick(View v) {
                joinGame(true);
            }
        });

		// add code here to invoke the application level logic
    }
    private void hostGame(boolean startWithHuman) 
    {
    	
    	boolean flag = ServerDetails.isInMulticastGroup();
    	if (flag==false)
    	{
    		WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
    		MulticastLock mcLock=null;
            if(wifi != null)
            {
                mcLock = wifi.createMulticastLock("mylock");
                mcLock.acquire();
            }
            Scan scanServer = new Scan();
            scanServer.set_server();
            //scanServer.search_server();
            //CharSequence text = scanServer.search_server();
            //Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
        	//toast.show();
    	} 
    	Intent myintent = new Intent(GuessASketch.this,CanvasDrawingActivity.class);
    	GuessASketch.this.startActivity(myintent);
    	/*
    	boolean flag = ServerDetails.isInMulticastGroup();
        conn = Connection.getInstanceOfConnection();
        ServerSocket sock = conn.openServerSocket();
        Intent myintent = new Intent(GuessASketch.this,CanvasDrawingActivity.class);
    	GuessASketch.this.startActivity(myintent);
    	*/
    }
    private void joinGame(boolean startWithHuman) 
    {
    	boolean flag = ServerDetails.isInMulticastGroup();
    	if (flag==false)
    	{
    		WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
    		MulticastLock mcLock=null;
            if(wifi != null)
            {
                mcLock = wifi.createMulticastLock("mylock");
                mcLock.acquire();
            }
            Scan scanServer = new Scan();
            if(!scanServer.set_client("172.23.24.205")){
            	Toast toast = Toast.makeText(getApplicationContext(), "Server not available", Toast.LENGTH_LONG);
        		toast.show();
        		return;
    		}
            //scanServer.search_server();
            //CharSequence text = scanServer.search_server();
            //Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
        	//toast.show();
    	}
    	Intent myintent = new Intent(GuessASketch.this,GuesserSideActivity.class);
    	GuessASketch.this.startActivity(myintent);
  /*  	
    	Socket socket;
        try{
        Log.d("ADClient","Connecting to server");
        socket = new Socket("172.23.5.83",5556);
        conn = Connection.getInstanceOfConnection();
        conn.setClientSocket(socket);
        Intent myintent = new Intent(GuessASketch.this,GuesserSideActivity.class);
    	GuessASketch.this.startActivity(myintent);
        }catch(Exception ex){
                ex.printStackTrace();
        }
*/
    }
}